angular.module('starter')
.controller("doctorHomeCtrl", function ($scope,$timeout, $stateParams, appBase, httpService) {
	$scope.skillBol = false;
	$scope.handleBol = true;
	$scope.DateNow = new Date().Format('yyyy-MM-dd');
	$scope.WeekNow = new Date().Format('W');
	var docId = $stateParams.docId || "";
	var detpId = $stateParams.detpId || "";
	var head_top = $(".doctor_home .head_top").outerHeight();
	var skill_box = $(".skill_box");
    //医生信息集合
	$scope.Doctor = [];
	$scope.DoctorParma = {};
	$scope.listTime = [];
	$scope.TodayTime = [];
	$scope.skillFn = function(){//点击展开收起
		
		if($scope.handleBol){
			$scope.skillBol = true;
			$scope.handleBol = false;
			$timeout(function(){
				var skill_boxH = skill_box.outerHeight();
				$(".doctor_home .container").css("top",head_top+skill_boxH);
			},50)
			
		}else{
			$scope.skillBol = false;
			$scope.handleBol = true;
			
			$(".doctor_home .container").css("top","2.14rem");
		}
	}
	//判断内容只有一行不显示展开按钮
	$scope.handleShow = false;
	var spanH = $(".doctor_home .skill_box span").outerHeight();
	var skillBoxH = $(".doctor_home .skill_box").outerHeight();
	if(spanH>skillBoxH){
		$scope.handleShow = true;
	}
	
	//tab切换
	$scope.navIndex = 0;
	$scope.navClick = function(idx){
		$scope.navIndex = idx;
	}
	
	$scope.weekIndex = 0;
	$scope.weekdayFn = function(idx){
	    $scope.weekIndex = idx;
	    if ($scope.Doctor.regScheduleInfos[idx])
	    {
	        $scope.listTime = $scope.Doctor.regScheduleInfos[idx];
	    }
	}

	$scope.$watch('$viewContentLoaded', function () {
	    // 你的代码
	    loadDoctor();
	});

	//$scope.$on('$ionicView.beforeEnter', function () {
	//    //console.log("返回页面");
	//    //这里只需要将需要的字段重新赋值就OK了
	//});

	//$scope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
	//    if (fromState.url == "placeOrder") {
	//        console.log("返回页面");
	//    }
	//});
 


    ///加载医生的排班信息和个人资料
	function loadDoctor() {
	  
	    httpService.parData.clear();
	    var parData = httpService.parData;
	    parData.deptId = detpId;
	    parData.doctorId = docId;
	    parData.hospitalCode = appBase.hospitalCode;

	    httpService.post(httpService.appWebApi.getDoctorHome,
          parData
          ).then(
          function (success) {
              var data = success.data;
              if (data.returnCode != "0") {
                  console.log(data.returnMsg)
                  return;
              }
              var map = {};//分组使用
              angular.forEach(data.returnData, function (modal) {
 
                  $scope.Doctor.doctorId = modal.doctorId;
                  $scope.Doctor.doctorName = modal.doctorName;
                  $scope.Doctor.doctorTitle = modal.doctorTitle;
                  $scope.Doctor.doctorRemark = modal.doctorRemark;
                  $scope.Doctor.doctorGender = modal.doctorGender;
                  $scope.Doctor.betterFor = modal.betterFor;
                  $scope.Doctor.urlPic = modal.urlPic || "img/face2.png";
                  $scope.Doctor.visitTimeInfo = modal.visitTimeInfo;
                  $scope.Doctor.branchName = modal.branchName;
                  $scope.Doctor.deptId = modal.deptId;
                  $scope.Doctor.deptName = modal.deptName;
                  if (!$scope.Doctor.regScheduleInfos)
                  {
                      $scope.DoctorParma = {
                          doctorId : modal.doctorId,
                          doctorName : modal.doctorName,
                          doctorTitle : modal.doctorTitle,
                          doctorRemark : modal.doctorRemark,
                          doctorGender : modal.doctorGender,
                          urlPic : modal.urlPic || "img/face2.png",
                          deptId : modal.deptId,
                          deptName : modal.deptName
                      };
                      $scope.Doctor.regScheduleInfos = [];
                  }
                  angular.forEach(modal.regScheduleInfos.regScheduleInfo, function (info) {

                      if (!map[info.regDate]) {
                          $scope.Doctor.regScheduleInfos.push({
                              regDate: info.regDate,
                              regWeekDay: info.regWeekDay,
                              regStatus: info.regStatus,
                              regFee: info.regFee,
                              treatFee: info.treatFee,
                              isTimeReg: info.isTimeReg,
                              listTime: [{
                                  beginTime: info.beginTime,
                                  endTime: info.endTime,
                                  regTotalCount: info.regTotalCount,
                                  regleaveCount: info.regleaveCount,
                                  scheduleType: info.scheduleType,
                                  regDate: info.regDate
                              }]
                          });
                          map[info.regDate] = 1;
                      } else {
                          for (var j = 0; j < $scope.Doctor.regScheduleInfos.length; j++) {
                              var dj = $scope.Doctor.regScheduleInfos[j];
                              if (dj.regDate == info.regDate) {
                                  $scope.Doctor.regScheduleInfos[j].listTime.push({
                                        beginTime: info.beginTime,
                                        endTime: info.endTime,
                                        regTotalCount:info.regTotalCount,
                                        regleaveCount: info.regleaveCount,
                                        scheduleType: info.scheduleType,
                                        regDate: info.regDate
                                  });
                                  break;
                              }
                          }
                      }

                      //if ($scope.TodayTime.length == 0) {
                      //    if ($scope.Doctor.regScheduleInfos[0]) {
                      //        $scope.TodayTime = $scope.Doctor.regScheduleInfos[0];
                      //    }
                      //}
                  })
              });

       
                  if ($scope.Doctor.regScheduleInfos[0]) {
                      var tempdata = [];
                      angular.forEach($scope.Doctor.regScheduleInfos[0].listTime, function (item, index) {
                          if (item.endTime > (new Date().Format('hh:mm:ss'))) {
                              tempdata.push(item);
                          }
                      });
                      if ($scope.Doctor.regScheduleInfos[0].regDate == (new Date().Format('yyyy-MM-dd')))
                      {
                          $scope.Doctor.regScheduleInfos[0].listTime = tempdata;
                      }
                      $scope.TodayTime = $scope.Doctor.regScheduleInfos[0];
                  }

              $scope.listTime = $scope.TodayTime;
          }
          , function (error) {
              console.log(error);
          })
	}

})
